package com.wits.platform.common.utils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import com.wits.platform.common.persistence.HibernatePropertyFilter;
import com.wits.platform.web.mvc.PageParam;

public class PageUtil<T> {

	private static Logger logger = LoggerFactory.getLogger(PageUtil.class);
	
	
	public static PageRequest buildPageRequest(PageParam page) {
		Sort sort =null;
		String sortName=page.getSortName();
		String sortOrder=page.getSortOrder();
		if (sortName != null && sortOrder!=null) {
			if (Direction.ASC.name().equalsIgnoreCase(sortOrder)) {
				sort = new Sort(Direction.ASC,sortName);
			} else {
				sort = new Sort(Direction.DESC,sortName);
			}
		}
		return new PageRequest(page.getPageNum() - 1, page.getNumPerPage(), sort);
	}
	
	public static <T> Specification<T> buildSpecification(Class<T> entityClass,Map<String, Object> filterParams) {
		Map<String, SearchFilter> filters = HibernatePropertyFilter.parse(filterParams);
		Specification<T> spec = DynamicSpecifications.bySearchFilter(filters.values(),entityClass);
		return spec;
	}
	
	
	/**
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public static int getPageStart(int pageNumber, int pageSize) {
		return (pageNumber - 1) * pageSize;
	}
	
	public static String getCountQuery(String sql){
		//order by影响count查询
		String lowerSql=sql.toLowerCase();
		int orderIndex=lowerSql.indexOf("order");
		if(orderIndex!=-1){
			sql=lowerSql.substring(0, orderIndex);
		}
		int index = lowerSql.indexOf("from");
		if(index != -1){
			return "select count(*) " + sql.substring(index);
		}
		logger.error("totalHql is failure",sql);
		return null;
	}

	
	public static <T> String toAjaxPageResult(Collection<T> list) {
		String items="[]";
		int size=0;
		if(list!=null){
			items=list.toString();
			size=list.size();
		}
		return "{\"success\":true,\"totalNum\":"+size+",\"items\":"+items+"}";
	}
	
	public static <T> String toAjaxPageResult(Page<T> listPage) {
		List<T> content=null;
		long totalNum=0;
		if(listPage!=null){
			content=listPage.getContent();
			totalNum=listPage.getTotalElements();
		}
		String items=content!=null?content.toString():"[]";
		return "{\"success\":true,\"totalNum\":"+totalNum+",\"items\":"+items+"}";
		
	}
	
	public static <T> String toAjaxPageResult(long totalElements, Object content) {
		String items=content!=null?content.toString():"[]";
		return "{\"success\":true,\"totalNum\":"+totalElements+",\"items\":"+items+"}";
		
	}

	public static <T> String toAjaxPageResult(Page<T> listPage,JsonConfig jsonConfig) {
		JSONArray arr=JSONArray.fromObject(listPage.getContent(),jsonConfig);
		return toAjaxPageResult(listPage.getTotalElements(),arr);
	}
	
	public static <T> String toAjaxPageResult(List<T> list,JsonConfig jsonConfig) {
		JSONArray arr=JSONArray.fromObject(list,jsonConfig);
		return toAjaxPageResult(list.size(),arr);
	}
	

	

	

}
